#include "sensor.h"


#define TZ_SENSOR_PORT        GPIOA
#define	TZ_SENSOR_PIN		 GPIO_PIN_11

#define	FUZI_PORT	GPIOB
#define	UP_FUZI_PIN	GPIO_PIN_2
#define	DW_FUZI_PIN	GPIO_PIN_1


void	InitialSensor(void)
{
    GPIO_InitType GPIO_InitStructure;
    /*Configure the GPIO pin as input floating*/
		GPIO_InitStruct(&GPIO_InitStructure);
		GPIO_InitStructure.Pin        = TZ_SENSOR_PIN;
		GPIO_InitStructure.GPIO_Mode  = GPIO_MODE_INPUT;// | GPIO_MODE_IT_FALLING;
		GPIO_InitStructure.GPIO_Pull  = GPIO_PULL_UP;
		GPIO_InitPeripheral(TZ_SENSOR_PORT, &GPIO_InitStructure);
	
		GPIO_InitStructure.Pin        = UP_FUZI_PIN | DW_FUZI_PIN;
		GPIO_InitPeripheral(FUZI_PORT, &GPIO_InitStructure);
}

void	TZ_Sensor_PowerOn(bool IsPowerOn)
{
	if (IsPowerOn)
	{
		TZPowerOn();
		delay_ms(50);
		machine.TZPowerOnTime = TZ_POWERON_TIME;
	}
	else
	{
		TZPowerOff();
	}
}

U8 Check_Water_Sensor(void)
{
	U8 water_status = WATER_ERROR;

	if (!machine.TZSensorPower)
		TZ_Sensor_PowerOn(true);
	Check_Sensor();	
	Error_Flg.BIT.Water_Err = NO;
	if (SensorNow.DW_FUZI == 1) //&& (SensorNow.TZ_Status == 0))
		water_status = WATER_LOW_LEVEL;
	else if ((SensorNow.DW_FUZI == 0) && (SensorNow.TZ_Status == 1))
		water_status = WATER_HIGH_LEVEL;
	else if ((SensorNow.DW_FUZI == 0) && (SensorNow.TZ_Status == 0))
		water_status = WATER_NORMAL_LEVEL;
	else if (SensorNow.UP_FUZI == 0)
		water_status = WATER_ERROR;
	if (water_status == WATER_ERROR)
		Error_Flg.BIT.Water_Err = YES;
	else
		Error_Flg.BIT.Water_Err = NO;
	return water_status;
}

void	Check_Sensor(void)
{
#ifdef  USE_TZ_SENSOR
	if (machine.TZSensorPower)
		SensorNow.TZ_Status = GPIO_ReadInputDataBit(TZ_SENSOR_PORT,TZ_SENSOR_PIN);
#endif	
	SensorNow.UP_FUZI = GPIO_ReadInputDataBit(FUZI_PORT,UP_FUZI_PIN);
	SensorNow.DW_FUZI = GPIO_ReadInputDataBit(FUZI_PORT,DW_FUZI_PIN);
}
